package data_management;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import android.util.Log;

import XMLManager.XML_P;
import XMLManager.XML_S;
import XMLManager.yXMLReader;
import XMLManager.yXMLWriter;

public class SettingData {
	private static SettingData me = new SettingData();
	static{
		me = new SettingData();
	}
	// constructor 
	public static SettingData getInstance(){
		return me;
	}
	
	private int [] padColor;
	private int sensitivity = 100;
	private int timeLimit=3000;
	
	private boolean lock_on   = false;
	private boolean lock_boot = true;
	
	private boolean firstExec = true;
	
	final private int min_sens = 50;
	final private int max_sens = 250;
	
	public void setTimeLimit(int timeLimit){ this.timeLimit = timeLimit; }
	public int timelimit(){ return timeLimit; }
	
	public int sens_limit(){	return max_sens - min_sens; }
	
	public void setr(int r){padColor[0] = r;}
	public int 	r(){ return padColor[0];}
	
	public void setg(int g){padColor[1] = g;}
	public int 	g(){ return padColor[1];}
	
	public void setb(int b){padColor[2] = b;}
	public int 	b(){ return padColor[2];}
	
	public void setPadColor(int r, int g, int b){ this.padColor[0] = r; this.padColor[1] = g;	this.padColor[2] = b;	}
	public int [] getPadColor(){ return padColor;}
	
	public void setSensitivity(int sensitivity){ this.sensitivity = sensitivity + min_sens;}
	public int getSensitivity(){ return sensitivity - min_sens; }
	
	public boolean isLock_on() 					{return lock_on;}
	public void    setLock_on(boolean lock_on)  {this.lock_on = lock_on;}
	
	public boolean isLock_boot() 				{return lock_boot;}
	public void setLock_boot(boolean lock_boot) {this.lock_boot = lock_boot;}
	
	private void init(){
		padColor = new int[3];
		padColor[0] = 242;
		padColor[1] = 49;
		padColor[2] = 240;
		sensitivity = 100;
	}
	
	private SettingData(){
		init();
		open();
		Log.e("myerror-class:", "lock_on: "+lock_on);
		save();
	}
	
	//destructor
	protected void finalize(){
		close();
	}
	//open DB
	public void open(){
		//open the xml file
		yXMLReader yread = new yXMLReader(XML_S.FILE_NAME);
		boolean isOpened = yread.open();
		Log.e("open1:", "before open");
		if(!isOpened) return ;
		Log.e("open2:", "after open");
		// get first
		int event;// = yread.event();
		
		String curTag = "";
		String curText = "";
		String p;
//		Log.e("doc1:", event+" start doc "+yread.START_DOCUMENT);
//		if(event != yread.START_DOCUMENT) return ;
		Log.e("doc2:", "start doc");
		while((event = yread.event())!=yread.END_DOCUMENT){
			// dealing tag
			if		( event == yread.START_TAG	) curTag = yread.getName();
			else if	( event == yread.END_TAG	) curTag = "";
			
			Log.e("tag:", curTag);
			// dealing pad color(R,G,B)
			if(curTag.equals(XML_S.PAD_COLOR)){
				padColor[0] = Integer.parseInt(yread.getAttr(XML_S.r));
				padColor[1] = Integer.parseInt(yread.getAttr(XML_S.g));
				padColor[2] = Integer.parseInt(yread.getAttr(XML_S.b));
			}
			
			// getting sensitivity
			else if(curTag.equals(XML_S.SENSITIVITY)){
				if(yread.event()!=yread.TEXT) break;
				
				p = yread.getText();
				sensitivity = Integer.parseInt(p);
			}
			
			// getting time limit
			else if(curTag.equals(XML_S.TIME_LIMIT)){
				if(yread.event()!=yread.TEXT) break;
				
				p = yread.getText();
				timeLimit = Integer.parseInt(p);
			}
			
			// getting lock screen attributes
			else if(curTag.equals(XML_S.LOCK)){
				//lock_boot = Boolean.parseBoolean(yread.getAttr(XML_S.onBoot));
				
				if(yread.event()!=yread.TEXT) break;
				
				p = yread.getText();
				lock_on = Boolean.parseBoolean(p);
			}			
			
		}
	}
	
	//close DB
	public void close(){
		yXMLWriter ywrite = new yXMLWriter(XML_S.ROOT, XML_S.FILE_NAME, XML_S.ENCODING_CODE);
		ywrite.newSettings(
				padColor, 
				sensitivity, 
				timeLimit, 
				lock_on, 
				lock_boot
		);
		ywrite.close();
	}
	
	public void save(){
		close();
	}
}